<template>
  <!-- 年级信息页面 -->
  <div class="app-container">
    <searchForm
      :formOptions="formOptions"
      @onSearch="onSearch"
      @onReset="onReset"
      :formLabelWidth="formLabelWidth"
      v-show="showSearch"
    />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:grade:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:grade:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:grade:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:grade:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table :data="roleList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" prop="name" align="center" />
      <el-table-column label="入校时间" prop="enrolTime" :show-overflow-tooltip="true" align="center" />
      <el-table-column label="毕业时间" prop="graduate" align="center" />
      <!-- <el-table-column label="年级分类" prop="gradeType" align="center">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.gradeType"></dict-tag>
        </template>
      </el-table-column>-->
      <el-table-column label="创建人" prop="createBy" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:class:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:grade:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      @close="CloseIn"
      width="60%"
      append-to-body
      destroy-on-close
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="年级名称" prop="name">
              <el-input v-model.trim="form.name" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入校时间" prop="enrolTime">
              <el-date-picker
                v-model="form.enrolTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
                style="width:100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="毕业时间" prop="graduate">
              <el-date-picker
                v-model="form.graduate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择毕业时间"
                style="width:100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <!-- 年级分类暂时不要 -->
          <!-- <el-col :span="12">
            <el-form-item label="年级分类" prop="gradeType">
              <el-select
                v-model="form.gradeType"
                placeholder="请选择年级分类"
                clearable
                style="width:100%"
              >
                <el-option
                  v-for="dict in dict.type.sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="12">
            <el-form-item label="创建人" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入创建人" maxlength="30" />
            </el-form-item>
          </el-col>-->
          <!-- <el-col :span="12">
            <el-form-item label="起始时间" prop="createTime">
              <el-date-picker
                v-model="form.createTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择起始时间"
                style="width:100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>-->
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  gradeList,
  addGrade,
  updataGrade,
  gradeInfor,
  delGrade
} from "@/api/information/gradeInfor";
import searchForm from "@/components/Search/searchForm.vue";
import Validators from "@/utils/validate.js";
export default {
  name: "GradeInfor",
  dicts: ["sys_normal_disable"],
  components: { searchForm },
  data() {
    return {
      formLabelWidth: "60px",
      formOptions: [
        {
          label: "名称",
          prop: "name",
          element: "el-input",
          placeholder: "请输入名称"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {
        name: undefined,
        enrolTime: undefined,
        graduate: undefined
      },
      name: undefined,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        name: [
          {
            required: true,
            text: "请输入年级名称",
            validator: Validators.isStr,
            trigger: "blur"
          }
        ],
        enrolTime: [
          { required: true, message: "请选择入校时间", trigger: "change" }
        ],
        graduate: [
          { required: true, message: "请选择毕业时间", trigger: "change" }
        ]
        // gradeType: [
        //   { required: true, message: "请选择年级分类", trigger: "change" }
        // ],
        // createBy: [
        //   { required: true, message: "创建人不能为空", trigger: "blur" }
        // ],
        // createTime: [
        //   { required: true, message: "请选择起始时间", trigger: "change" }
        // ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 获取搜索表单提交的数据
    onSearch(val) {
      // 查询条件和分页数据合并
      this.queryParams = Object.assign(this.queryParams, val);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    onReset(data) {
      this.queryParams = Object.assign(this.queryParams, data);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      gradeList(this.queryParams).then(response => {
        this.roleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.form = {};
    },
    // 清除表单
    CloseIn() {
      this.form = {};
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.name = selection.map(item => item.name);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加年级信息";
    },

    /** 修改按钮操作 */
    async handleUpdate(row) {
      await gradeInfor(row.id).then(res => {
        this.open = true;

        this.form = res.data;
        this.title = "修改信息";
      });
    },
    /** 选择角色权限范围触发 */
    dataScopeSelectChange(value) {
      if (value !== "2") {
        this.$refs.dept.setCheckedKeys([]);
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updataGrade(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGrade(this.form).then(response => {
              console.log(response, "新增成功");
              this.$modal.msgSuccess("新增成功");
              this.form = {};
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.id || this.ids;
      this.name = row.name || this.name;
      this.$modal
        .confirm('是否确认删除年级名称"' + this.name + '"的数据项？')
        .then(function() {
          return delGrade(roleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/basic/grade/export",
        {
          ...this.queryParams
        },
        `年级信息_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>